﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms.Integration;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using System.IO;

namespace wpfCode
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        //private void submitText_Click(object sender, System.Windows.RoutedEventArgs e)
        //{

        //    System.Drawing.Bitmap bt;
        //    string st = "123" + this.codeText.Text + "012345678901234567890123456789";
        //    if (st.Length == 43)
        //    {
        //        string enCodeString = st;
        //        Grid grid = new Grid();
        //        grid.Name = "grid1";
        //        RowDefinition row1 = new RowDefinition();   //实例化一个Grid行
        //        ColumnDefinition co1 = new ColumnDefinition();    //实例化一个Grid列

        //        //将实例化的行和列添加到Grid中
        //        grid.RowDefinitions.Add(row1);
        //        grid.ColumnDefinitions.Add(co1);
        //        grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

        //        WindowsFormsHost wfh = new WindowsFormsHost();
        //        PictureBox p = new PictureBox();
        //        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
        //        p.Image = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
        //        wfh.Child = p;
        //        grid.Children.Add(wfh);
        //        Grid.SetColumn(wfh, 0);
        //        this.imageList.Children.Add(grid);
        //        Grid.SetRow(grid, 1);
        //    }
        //    else
        //    {
        //        System.Windows.MessageBox.Show("数据不正确！！！");
        //    }
        //}

        private void submitBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            System.Drawing.Bitmap bt;
            string enCodeString = this.codeText.Text;
            Grid grid = new Grid();
            grid.Name = "grid2";
            RowDefinition row1 = new RowDefinition();   //实例化一个Grid行
            ColumnDefinition co1 = new ColumnDefinition();    //实例化一个Grid列
            //将实例化的行和列添加到Grid中
            grid.RowDefinitions.Add(row1);
            grid.ColumnDefinitions.Add(co1);
            grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

            WindowsFormsHost wfh = new WindowsFormsHost();
            PictureBox p = new PictureBox();
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            p.Image = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
            wfh.Child = p;
            grid.Children.Add(wfh);
            Grid.SetColumn(wfh, 0);
            this.imageList.Children.Add(grid);
            Grid.SetRow(grid, 1);

            string[] texts = enCodeString.Split(',');
            if (texts.Length > 1)
            {
                int col = 3;
                int row = texts.Length > 3 ? texts.Length / 3 : 1;
                setGrid(col, row, texts);
            }
        }
        private void submitsText_Copy_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
            //op.InitialDirectory = lblSavePath.Text;//默认的打开路径
            op.RestoreDirectory = true;
            op.Filter = " 文本文件(*.txt)|*.txt";
            if ((bool)op.ShowDialog())
            {
                FileStream fs;
                if (File.Exists(op.FileName))
                {
                    fs = new FileStream(op.FileName, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
                    string text = sr.ReadToEnd();
                    codeText_Copy.Text = sr.ReadToEnd();
                    if (!string.IsNullOrEmpty(text))
                    {
                        string[] texts = text.Split(',');
                        if (texts.Length > 0)
                        {
                            int col = 3;
                            int row = texts.Length > 3 ? texts.Length / 3 : 1;
                            setGrid(col, row, texts);
                        }
                    }
                    sr.Close();
                }

            }
        }

        private void setGrid(int col, int row, string[] texts)
        {
            Grid grid = new Grid();
            grid.Name = "grid2";
            grid.Width =600;
            // 定义行和列
            for (int i = 0; i < row; i++)
            {
                RowDefinition row1 = new RowDefinition();   //实例化一个Grid行
                grid.RowDefinitions.Add(row1);
            }
            for (int i = 0; i < col; i++)
            {
                ColumnDefinition co11 = new ColumnDefinition();    //实例化一个Grid列
                grid.ColumnDefinitions.Add(co11);
            }

            grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

            // 生成数据
            for (int m = 0; m < texts.Length; m++)
            {
                // 列
                int a = m % 3;
                int b = m / 3;

                var border = new Border();
                border.BorderBrush = new SolidColorBrush(Colors.Red);
                border.BorderThickness = new Thickness(1, 1, 1, 1);


                System.Drawing.Bitmap bt;
                string st = "123" + texts[m].Replace("\r\n", "").Replace("\r", "").Replace("\n","") + "012345678901234567890123456789";
                string enCodeString = st;
                WindowsFormsHost wfh = new WindowsFormsHost();
                PictureBox p = new PictureBox();
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                p.Image = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
                wfh.Child = p;
                border.Child = wfh;
                grid.Children.Add(border);
                Grid.SetColumn(border, a);
                Grid.SetRow(border, b);
                //grid.Children.Add(wfh);
                //Grid.SetColumn(wfh, a);
                //Grid.SetRow(wfh, b);

            }
            scrool.Content = grid;
            //this.imageList.Children.Add(grid);
            //Grid.SetColumn(grid, 0);
            //Grid.SetRow(grid, 1);
        }
    }
}
